package com.wmlive.hhvideo.heihei.mainhome.adapter;

import android.view.View;
import android.view.ViewGroup;

import com.wmlive.hhvideo.heihei.beans.main.Comment;
import com.wmlive.hhvideo.heihei.mainhome.viewholder.CommentListViewHolder;
import com.wmlive.hhvideo.utils.MyClickListener;
import com.wmlive.hhvideo.utils.imageloader.GlideLoader;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshAdapter;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;

import java.util.List;

import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 6/19/2017.
 * 评论列表的Adapter
 */

public class CommentListAdapter extends RefreshAdapter<CommentListViewHolder, Comment> {
    private OnCommentItemClickListener commentItemClickListener;

    public CommentListAdapter(List<Comment> list, RefreshRecyclerView refreshView) {
        super(list, refreshView);
    }

    @Override
    public CommentListViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new CommentListViewHolder(parent, R.layout.item_video_comment);
    }

    @Override
    public void onBindHolder(CommentListViewHolder holder, final int position, Comment data) {
        data = (data == null ? new Comment() : data);
        GlideLoader.loadCircleImage(data.getUser() == null ? null : data.getUser().getCover_url(), holder.ivAvatar, R.drawable.find_bg_user_default);
        holder.tvName.setText(data.getUser() == null ? "" : data.getUser().getName());
        holder.tvComment.setText(data.getTitle());
        final Comment finalData = data;
        holder.ivAvatar.setOnClickListener(new MyClickListener() {
            @Override
            protected void onMyClick(View v) {
                if (null != commentItemClickListener) {
                    commentItemClickListener.onAvatarClick(finalData.getUser_id());
                }
            }
        });
        holder.tvName.setOnClickListener(new MyClickListener() {
            @Override
            protected void onMyClick(View v) {
                if (null != commentItemClickListener) {
                    commentItemClickListener.onAvatarClick(finalData.getUser_id());
                }
            }
        });
//        holder.tvComment.setOnClickListener(new MyClickListener(){
//            @Override
//            protected void onMyClick(View v) {
//
//            }
//        });
        holder.getRootView().setOnClickListener(new MyClickListener() {
            @Override
            protected void onMyClick(View v) {
                if (null != commentItemClickListener) {
                    commentItemClickListener.onNameClick(position, finalData);
                }
            }
        });
    }

    public void refreshItemDelete(int position, long commentId) {
        if (position > -1 && position < getDataContainer().size()) {
            if (getDataContainer().get(position).getId() == commentId) {
                getDataContainer().remove(position);
                notifyItemRemoved(position);
            }
        }
    }

    public void setCommentItemClickListener(OnCommentItemClickListener commentItemClickListener) {
        this.commentItemClickListener = commentItemClickListener;
    }

    public interface OnCommentItemClickListener {
        void onAvatarClick(long userId);

        void onNameClick(int position, Comment comment);
    }
}
